/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package maps;

import java.awt.Color;
import java.awt.Graphics2D;
import java.util.ArrayList;

/**
 *
 * @author menderleit
 */
public class DungeonMap {

    private int width;
    private int height;
    private int tileSize;
    private ArrayList<DungeonRoom> rooms;
    private int numRooms;
    private MapCell[][] cells;

    public DungeonMap(int mapWidth, int mapHeight, int tileSize) {
        width = mapWidth;
        height = mapHeight;
        this.tileSize = tileSize;
        rooms = new ArrayList();
        numRooms = 0;
        if (width % 2 == 0) {
            width--;
        }
        if (height % 2 == 0) {
            height--;
        }
        cells = new MapCell[width][height];

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                cells[x][y] = new MapCell();
            }
        }
    }

    public void removeEntrance(int x, int y) {
        for (int i = 0; i < rooms.size(); i++) {
            DungeonRoom r = rooms.get(i);
            for (int j = 0; j < r.getNumEntrances(); j++) {
                if (x == r.getEntrance(j).x && y == r.getEntrance(j).y) {
                    r.removeEntrance(j);
                }
            }
        }
    }

    public DungeonRoom addRoom(int x, int y, int w, int h) {
        DungeonRoom r = new DungeonRoom(x, y, w, h);
        rooms.add(r);
        numRooms++;
        return r;
    }

    public void removeRoom(DungeonRoom r) {
        if(rooms.remove(r)) {
            numRooms--;
        }
    }
    
    public ArrayList<DungeonRoom> getRooms() {
        return rooms;
    }

    public DungeonRoom getRoom(int roomNr) {
        DungeonRoom r = null;
        roomNr--;
        if (roomNr >= 0 && roomNr < rooms.size()) {
            r = rooms.get(roomNr - 1);
        }
        return r;
    }

    public int getNumRooms() {
        return numRooms;
    }

    public void clear() {
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                cells[x][y].clear();
            }
        }
        rooms.clear();
        numRooms = 0;
    }

    public void setBlocked(int x, int y, boolean blocked) {
        if(x >= 0 && x < width && y >= 0 && y < height) {
            cells[x][y].setBlocked(blocked);
        }
    }
    
    public boolean isBlocked(int x, int y) {
        boolean blocked = false;
        if(x >= 0 && x < width && y >= 0 && y < height) {
            blocked = cells[x][y].isBlocked();
        }
        return blocked;
    }
    
    public boolean isRoom(int x, int y) {
        boolean b = false;
        if (x >= 0 && x < width && y >= 0 && y < height) {
            b = cells[x][y].isRoom();
        }
        return b;
    }

    public void setRoom(int x, int y, boolean room) {
        if (x >= 0 && x < width && y >= 0 && y < height) {
            cells[x][y].setRoom(room);
            if (!room) {
                cells[x][y].setRoomID(0);
            }
        }
    }

    public void setRoom(int x, int y, int roomID) {
        if (x >= 0 && x < width && y >= 0 && y < height) {
            cells[x][y].setRoom(true);
            cells[x][y].setRoomID(roomID);
        }
    }

    public boolean isPerimiter(int x, int y) {
        boolean b = false;
        if (x >= 0 && x < width && y >= 0 && y < height) {
            b = cells[x][y].isPerimiter();
        }
        return b;
    }

    public void setPerimiter(int x, int y, boolean perimiter) {
        if (x >= 0 && x < width && y >= 0 && y < height) {
            cells[x][y].setPerimiter(perimiter);
        }
    }

    public boolean isEntrance(int x, int y) {
        boolean b = false;
        if (x >= 0 && x < width && y >= 0 && y < height) {
            b = cells[x][y].isEntrance();
        }
        return b;
    }

    public void setEntrance(int x, int y, boolean entrance) {
        if (x >= 0 && x < width && y >= 0 && y < height) {
            cells[x][y].setEntrance(entrance);
        }
    }

    public boolean isCorridor(int x, int y) {
        boolean b = false;
        if (x >= 0 && x < width && y >= 0 && y < height) {
            b = cells[x][y].isCorridor();
        }
        return b;
    }

    public void setCorridor(int x, int y, boolean corridor) {
        if (x >= 0 && x < width && y >= 0 && y < height) {
            cells[x][y].setCorridor(corridor);
        }
    }

    public void clear(int x, int y) {
        if (x >= 0 && x < width && y >= 0 && y < height) {
            cells[x][y].clear();
        }
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public void generate(DungeonOptions opts) {
        DungeonGenerator.generate(this, opts);
    }

    public void draw(Graphics2D g, int xPos, int yPos) {
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                g.setColor(Color.lightGray);
                MapCell c = cells[x][y];
                if (c.isRoom()) {
                    g.setColor(Color.darkGray);
                }
                if(c.isCorridor() && !c.isRoom()) {
                    g.setColor(Color.darkGray);
                }
                if (c.isEntrance()) {
                    g.setColor(Color.gray);
                }
                if (c.isPerimiter()) {
                    g.setColor(Color.lightGray);
                }
                if (c.isBlocked()) {
                    g.setColor(Color.lightGray);
                }
                g.fillRect(x * tileSize + xPos, y * tileSize + yPos, tileSize, tileSize);
            }
        }
    }
}
